package com.jwdemo
{
	// **** THIS IS NO LONGER USED - SEE GameObjectManager.as our generic Manager****
	
	// Simple Bullet Manager - using Double Linked List Class
	
	import com.jwdemo.LinkedList;
	
	public class BulletManager
	{
		private static var bMan:BulletManager  ;
		
		private var bulletlist : LinkedList ;
		
		
		
		function BulletManager()
		{
			bulletlist	=	new LinkedList() ;
			
		}
		
		
		public static function Init() : void
		{
			bMan	=	new BulletManager() ;
		}
		
		private static function add(bullet:Bullet) : Bullet
		{
			bMan.bulletlist.add(bullet) ;
			return bullet
		}

		
		
		public static function Update(dt:Number) : void
		{
			var removeList:LinkedList	=	new LinkedList() ; 
			var list:LinkedList 		=	bMan.bulletlist	;

			for (var it:Object = list.Iterator() ; it.hasElements() ; it.next())
			{
				var node:Node = it.getCurrent() ;
				
				var bullet:Bullet	= node.getData() ;
				if (!bullet.isAlive())
				{
					bullet.update(dt) ;
				} else
				{
					removeList.add({list:list,element:node}) ;
				}	
				
			}
			
			// finished updating - now remove those that have finished.
			for (var itr:Object = removeList.Iterator(true) ; itr.hasElements() ; itr.next())
			{
				var removeInfo:Object = itr.getCurrent() ;
				removeInfo.list.remove(removeInfo.element) ;
				removeInfo.element.getData().applyDamage() ;
			}

			
		}

		public static function Render(rHelper:RenderHelper) : void
		{
			var list:LinkedList	=	bMan.bulletlist	 ;
			
			for (var it:Object = list.Iterator(true) ; it.hasElements() ; it.next())
			{
				var bullet:Bullet = it.getCurrent() ;
				bullet.render(rHelper)
			}
			
			
		}

		//BulletManager.Fire(this,kDAMAGE,kVELOCITY,this.GetFiringDirection(),kDURATION,animation,1,10) ;
		// Note - TODO Change Enemy to type InfMovableObject for 'owner'
		public static function Fire(	owner:Enemy, 
										damage:Number, 
										velocity:Number, direction:Number, 
										duration: Number, 
										animation:TextureAnim,
										scale:Number = 1, 
										radius:Number = 10) : Bullet
		{
			//BulletManager.Fire				=	function(owner,damage,vel,angle,duration,animation,scale,radius)
			return BulletManager.add(new Bullet(owner,damage,velocity,direction,duration,animation,scale,radius)) ;
		}

		
	
	}
}